package com.appspot.peachykene;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HerbFact implements Comparable<HerbFact> {
	private static final String BLANK_IMAGE = "/img/spacer.gif";
	private String en_name;
	private String nl_name;
	private String la_name;
	private String image;	
	private String en_description_short;
	private String nl_description_short;
	private final Set<String> en_conditions;
	private final Set<String> nl_conditions;

	public HerbFact() {
		nl_conditions = new HashSet<String>();
		en_conditions = new HashSet<String>();
	}

	public void setName(String name, String lang) {
		if("nl".equals(lang)) {
		    nl_name = name;
		} else {
			en_name = name;
		}
	}
	public String getName(String lang) { 
		if("nl".equals(lang) && nl_name != null) {
			return nl_name;
		} else {
			return en_name;
		}
	}
	
	public void setLatinName(String name) { this.la_name = name; }
	public String getLatinName() { return la_name; }
	
	public void setImage(String image) { this.image = image; }
	public String getImage() { return (image!=null) ? image : BLANK_IMAGE; }
	
	public String getDescriptionShort(String lang) { 
		if("nl".equals(lang)  && nl_description_short != null) {
			return nl_description_short;
		} else {
			return en_description_short;
		}
	}
	
	public void setDescriptionShort(String description, String lang) {
		if("nl".equals(lang)) {
			this.nl_description_short = description;
		} else {
			this.en_description_short = description;
		}
	}

	public boolean addCondition(String condition, String lang) {
		if(condition == null || condition.isEmpty()) return false;
		return getConditionsInternal(lang, false).add(condition); 
	}
	
	public boolean addAllConditions(Collection<String> conditions, String lang) { 
		return getConditionsInternal(lang, false).addAll(conditions); 
	}
	
	private Collection<String> getConditionsInternal(String lang, boolean fallback) {
		if("nl".equals(lang) && ! nl_conditions.isEmpty()) {
			return nl_conditions;
		} else {
			return en_conditions;
		}
	}
	public Collection<String> getConditions(String lang) { return Collections.unmodifiableCollection(getConditionsInternal(lang, true)); }
    public String getConditionsAsString(String lang) {
    	StringBuilder sb = new StringBuilder();
    	
    	for(Iterator<String> iter = getConditionsInternal(lang, true).iterator(); iter.hasNext();) {
    		sb.append(iter.next());
    		if(iter.hasNext()) {
    			sb.append(",");
    		}    		
    	}
    	return sb.toString();
    }
	/**
	 * The end goal is something like this:
	 * <div class="result">
     *   <img src="/img/fennel_133_200.jpg" height="200" alt="Image Unavailable"/>
     *     <ul>
     *      <li><h4>Fennel<i>(latin name)</i></h4></li>
     *      <li><text><b>Description:</b> Fennel</text></li>
     *      <li><text><b>Treats:</b> sore throat, cough</text></li>
     *     </ul>
     * </div>
     * Note: All string must be json escaped so instead of just \" use \\\"
	 * @param sb the html is added to this buffer, no newlines allowed.
	 * @param lang the language to render
	 */
	public void toHtmlFragment(StringBuilder sb, String lang) {
		sb.append("<div class=\\\"result\\\">")
		  .append("<img src=\\\"").append(getImage()).append("\\\" alt=\\\"Image Unavailable\\\" height=\\\"200\\\"/>")
		  .append("<ul><li><h4>").append(getName(lang)).append(" <i>(").append(getLatinName()).append(")</i> ").append("</h4></li>")
		  .append("<ul><li><text><b>").append(Rb.translate("Description", lang)).append(":</b> ").append(getDescriptionShort(lang)).append("</text></li>")
		  .append("<ul><li><text><b>").append(Rb.translate("Properties", lang)).append(":</b> ").append(getConditionsAsString(lang)).append("</text></li>")
		  .append("</ul></div>");
	}
	
	public static void noResultsHtmlFragment(StringBuilder sb, String lang) {
		sb.append("<div class=\\\"result\\\"><ul><li><h4 class=\\\"failed\\\">");
		sb.append(Rb.translate("ZERO_RESULTS", lang));		
		sb.append("</h4></li></ul></div>");
	}
	
	public void toLinkHtmlFragment(StringBuilder sb, String lang) {
		String name = getName(lang);
		sb.append("<a href=\\\"javascript:void(0)\\\" name=\\\"").append(name).append("\\\" class=\\\"herb_link\\\">").append(name)
		  .append(" <i>(").append(getLatinName()).append(")</i>")
		  .append("</a>");		
	}
	
	@Override
	public int hashCode() { return en_name.hashCode(); }
	@Override
	public boolean equals(Object rhs) { return (rhs instanceof HerbFact) ? ((HerbFact) rhs).getName("en").equals(getName("en")) : false; }

	@Override
	public int compareTo(HerbFact rhs) {
		return en_name.compareTo(rhs.getName("en"));
	}
}
